﻿package com.game.view.layer.gameMap
{
	import AssetClass.*;
	
	import com.dusk.util.ResUtil;
	import com.game.manager.GameSceneManager;
	import com.game.manager.NotificationManager;
	import com.game.view.layer.LayerBase;
	import com.game.view.layer.SelectArchive;
	
	
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.text.TextField;
	import flash.utils.Timer;
	
	public class MapMenu extends LayerBase
	{
		
		public function MapMenu():void
		{
			super();
		}
		
		public var buySkillBtn:SimpleButton;
		public var furnaceBtn:SimpleButton;
		public var microShopBtn:SimpleButton;
		public var saveGameBtn:SimpleButton;
		public var saveInterval:TextField;
		public var backToMenuBtn:SimpleButton;
		public var gameSettingBtn:SimpleButton;
		public var gameTaskBtn:SimpleButton;
		public var gardenBtn:SimpleButton;
		public var scoreShopBtn:SimpleButton;
		public var endlessMode:SimpleButton;
		
		public var sorryBag:MovieClip;
		private var saveTimer:Timer;
		private var saveIntervalCount:int = 0;
		
		override protected function beforeAdded():void
		{
			this.saveTimer = new Timer(1000);
			this.saveTimer.start();
			this.saveTimer.addEventListener(TimerEvent.TIMER, this.__timer);
			var mc:MovieClip = ResUtil.getLayerView("MapMenu") as MovieClip;
			
			buySkillBtn = mc.getChildByName("buySkillBtn") as SimpleButton;
			furnaceBtn = mc.getChildByName("furnaceBtn") as SimpleButton;
			microShopBtn = mc.getChildByName("microShopBtn") as SimpleButton;
			saveGameBtn = mc.getChildByName("saveGameBtn") as SimpleButton;
			saveInterval = mc.getChildByName("saveInterval") as TextField;
			backToMenuBtn = mc.getChildByName("backToMenuBtn") as SimpleButton;
			gameSettingBtn = mc.getChildByName("gameSettingBtn") as SimpleButton;
			gameTaskBtn = mc.getChildByName("gameTaskBtn") as SimpleButton;
			gardenBtn = mc.getChildByName("gardenBtn") as SimpleButton;
			scoreShopBtn = mc.getChildByName("scoreShopBtn") as SimpleButton;
			endlessMode = mc.getChildByName("endlessMode") as SimpleButton;
			
			sorryBag = mc.getChildByName("sorryBag") as MovieClip;
			addChild(mc);
		}
		
		private function intoBuySkill(param1:*):void
		{
			new RoleSkillInter();
			GameSceneManager.getIns().clearGameMap();
		}
		
		override protected function onAdded():void
		{
			this.buySkillBtn.addEventListener(MouseEvent.CLICK, this.intoBuySkill);
			this.saveGameBtn.addEventListener(MouseEvent.CLICK, this.saveGame);
			this.furnaceBtn.addEventListener(MouseEvent.CLICK, this.intoFurnace);
			this.microShopBtn.addEventListener(MouseEvent.CLICK, this.intoMicroShop);
			this.backToMenuBtn.addEventListener(MouseEvent.CLICK, this.toGameMenu);
			this.gardenBtn.addEventListener(MouseEvent.CLICK, this.intoGarden);
			this.gameSettingBtn.addEventListener(MouseEvent.CLICK, this.intoGameSetting);
			this.gameTaskBtn.addEventListener(MouseEvent.CLICK, this.intoGameTask);
			if (this.gc.userData.getSorryTime == 0)
			{
				this.sorryBag.visible = true;
				this.sorryBag.buttonMode = true;
				this.sorryBag.play();
			}
			else
			{
				this.sorryBag.visible = false;
				this.sorryBag.stop();
			}
		}
		
		override protected function onRemoved():void
		{
			this.buySkillBtn.removeEventListener(MouseEvent.CLICK, this.intoBuySkill);
			this.saveGameBtn.removeEventListener(MouseEvent.CLICK, this.saveGame);
			this.furnaceBtn.removeEventListener(MouseEvent.CLICK, this.intoFurnace);
			this.microShopBtn.removeEventListener(MouseEvent.CLICK, this.intoMicroShop);
			this.gameSettingBtn.removeEventListener(MouseEvent.CLICK, this.intoGameSetting);
			this.gameTaskBtn.removeEventListener(MouseEvent.CLICK, this.intoGameTask);
			this.backToMenuBtn.removeEventListener(MouseEvent.CLICK, this.toGameMenu);
			this.gardenBtn.removeEventListener(MouseEvent.CLICK, this.intoGarden);
			this.saveTimer.removeEventListener(TimerEvent.TIMER, this.__timer);
			this.saveTimer.stop();
			this.saveTimer = null;
		}
		
		private function intoFurnace(param1:MouseEvent):void
		{
			return;
//			new FurnaceInter();
		}
		
		private function intoMicroShop(param1:MouseEvent):void
		{
			return;
//			new MicroShopInter();
		}
		
		private function intoGarden(param1:MouseEvent):void
		{
			NotificationManager.tellNotOpen();
			return;
//			new GardenInter();
		}
		
		private function saveGame(param1:MouseEvent):void
		{
			var ui:SelectArchive;
			if (this.gc.isNew)
			{
				ui = new SelectArchive();
				ui.state = "save";
				this.addChild(ui);
				NotificationManager.alert("首次进入游戏请选择存档");
			}
			else if (!this.gc.isNew && this.gc.saveId == -1)
			{
				ui = new SelectArchive();
				ui.state = "save";
				this.addChild(ui);
				NotificationManager.alert("请选择移植的目标存档");
			}
			else
			{
				gc.memory.saveGame(this.gc.saveId, true);
			}
			this.saveIntervalCount = 10;
		}
		
		private function __timer(param1:TimerEvent):void
		{
			if (this.saveIntervalCount > 0)
			{
				--this.saveIntervalCount;
				this.saveInterval.text = "(" + this.saveIntervalCount.toString() + ")";
				this.saveGameBtn.enabled = false;
				this.saveGameBtn.mouseEnabled = false;
			}
			else
			{
				this.saveGameBtn.enabled = true;
				this.saveGameBtn.mouseEnabled = true;
				this.saveInterval.text = "";
			}
		}
		
		private function toGameMenu(param1:MouseEvent):void
		{
			GameSceneManager.getIns().switchScene(GameSceneManager.GAME_MENU);
		}
		
		private function intoGameSetting(param1:MouseEvent):void
		{
			new SettingInter();
		}
		
		private function intoGameTask(param1:MouseEvent):void
		{
			return;
			new TaskInter();
		}
	}
}
